package com.uzai.common.utils;

import com.uzai.common.dto.app.ContentAnalysisDto;
import com.uzai.common.enums.PlatformTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: xiaoyuexl
 * @Date: 2023/5/26 18:13
 * @Description: App正则表达式工具类
 */
public final class AppRegularUtil {

    /**
     * 转链内容识别
     * @param content
     * @return
     */
    public static List<ContentAnalysisDto> contentAnalysis(String content) {
        List<ContentAnalysisDto> list = new ArrayList<>();

        String[] contents = content.split("\n");

        for (String valContent : contents) {
            if (StringUtils.isBlank(valContent)) {
                continue;
            }
            Boolean isFindTag = false;

            Pattern pattern = Pattern.compile("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]");
            Matcher matcher = pattern.matcher(valContent);
            while (matcher.find()) {
                String url = matcher.group();
                //京东链接
                String[] jdUrlTagArr = new String[] {"jd.com","yiyaojd.com","jkcsjd.com","jingxi.com","3.cn"};
                for (int i = 0;i < jdUrlTagArr.length;i++) {
                    String domain = jdUrlTagArr[i];
                    if (url.contains(domain)) {
                        ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                        contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_JD);
                        contentAnalysisDto.setValue(url);
                        list.add(contentAnalysisDto);
                        isFindTag = true;
                    }
                }
                //拼多多链接
                String[] pddUrlTagArr = new String[] {"yangkeduo.com","m.ishangtong.com","pinduoduo.com"};
                for (int i = 0; i < pddUrlTagArr.length;i++) {
                    String domain = pddUrlTagArr[i];
                    if (url.contains(domain)) {
                        ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                        contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_PDD);
                        contentAnalysisDto.setValue(url);
                        list.add(contentAnalysisDto);
                        isFindTag = true;
                    }
                }
                //唯品会链接
                String[] vphUrlTagArr = new String[] {"vip.com","vipglobal.hk"};
                for (int i = 0; i < vphUrlTagArr.length;i++) {
                    String domain = vphUrlTagArr[i];
                    if (url.contains(domain)) {
                        ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                        contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_VPH);
                        contentAnalysisDto.setValue(url);
                        list.add(contentAnalysisDto);
                        isFindTag = true;
                    }
                }
                //淘宝链接
                String[] tbUrlTagArr = new String[] {"taobao.com","tmall.com","s.click.taobao","tmall.hk","zmnxbc.com","yukhj.com","detail.yao.95095.com","m.tb.cn","diantao.cn"};
                for (int i = 0; i < tbUrlTagArr.length;i++) {
                    String domain = tbUrlTagArr[i];
                    if (url.contains(domain)) {
                        ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                        contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_TAOBAO);
                        contentAnalysisDto.setValue(url);
                        list.add(contentAnalysisDto);
                        isFindTag = true;
                    }
                }
                //抖音链接
                String[] dyUrlTagArr = new String[] {"douyin.com"};
                for (int i = 0; i < dyUrlTagArr.length;i++) {
                    String domain = dyUrlTagArr[i];
                    if (url.contains(domain)) {
                        ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                        contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_DY);
                        contentAnalysisDto.setValue(url);
                        list.add(contentAnalysisDto);
                        isFindTag = true;
                    }
                }
                //快手链接

            }

            if (isFindTag) {
                continue;
            }

            //淘宝口令
            pattern = Pattern.compile("[0-9a-zA-Z\\s\\-\\(\\)\\\\\\/\\:\\$￥]{15,}");
            matcher = pattern.matcher(valContent);
            while (matcher.find()) {
                ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_TAOBAO);
                contentAnalysisDto.setValue(matcher.group());
                list.add(contentAnalysisDto);
            }
            //


            //抖音口令 ##tcZ4MFk5IC8##[抖音口令]
            if (valContent.contains("抖音口令")) {
                pattern = Pattern.compile("##([0-9a-zA-Z]{11})##");
                matcher = pattern.matcher(valContent);
                while (matcher.find()) {
                    ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                    contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_DY);
                    contentAnalysisDto.setValue(matcher.group());
                    list.add(contentAnalysisDto);
                }
            } else {
                pattern = Pattern.compile("(##X[-0-9a-zA-Z]+?##)");
                matcher = pattern.matcher(valContent);
                while (matcher.find()) {
                    ContentAnalysisDto contentAnalysisDto = new ContentAnalysisDto();
                    contentAnalysisDto.setPlatform(PlatformTypeEnum.PT_KS);
                    contentAnalysisDto.setValue(matcher.group());
                    list.add(contentAnalysisDto);
                }
            }
        }

        return list;
    }

}
